package com.rykj.qxj.rest;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.rykj.qxj.annotation.EnableSystemLog;
import com.rykj.qxj.util.IDUtils;
import com.rykj.qxj.util.IoUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;


@Log4j
@Controller
@RequestMapping("/v1/file")
@Api(tags="文件操作")
public class FileUploadController {
	
	@Value("${file-upload-path}")  
	private String serverPath;
	
	@Value("${file-upload-accept-prefixs}")
	private String acceptPrefixs ;

	@PostMapping("/upload")
	@ResponseBody
	@ApiOperation(value="文件上传",notes ="文件上传")
	//@EnableSystemLog(value="文件上传",fullPath ="/v1/file/upload")
	public String upload(@RequestParam("file") MultipartFile file) {

		if (file.isEmpty()) {
			return "文件为空";
		}
		String prefix = file.getName().substring(file.getName().lastIndexOf(".")+1);      
		if(StringUtils.contains(acceptPrefixs, prefix)){
			return "系统不接受["+prefix+"]类型的文件";
		}
		
		// 获取文件名
		String fileOldName = file.getOriginalFilename();
		//重新生成文件名称
		String fileName = IDUtils.genFileName();
		// 获取文件的后缀名
		String suffixName = fileOldName.substring(fileOldName.lastIndexOf("."));
		fileName = fileName+suffixName;
		
		if(log.isInfoEnabled()){
			log.info("上传的文件名为：" + fileName);
		}
		//根据日期建文件夹
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String dateStr = sdf.format(new Date());
		// 文件上传路径
		//String filePat"+dateStr+"/";
		String filePath = serverPath+dateStr+"/";
		//文件存储地址
		String path = filePath + fileName;
		File dest = new File(path);
		// 检测是否存在目录
		if (!dest.getParentFile().exists()) {
			dest.getParentFile().mkdirs();
		}
		try {
			file.transferTo(dest);
			//存储在pc的绝对路径
			//return path;
			//web要显示的路径
			return "/upload/"+dateStr+"/"+fileName;
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	@PostMapping("/download")
	@ApiOperation(value="下载文件",notes ="下载文件")
	@ApiImplicitParams({
       @ApiImplicitParam(name = "path", value = "文件路径",required = true,paramType = "query")
 })
	public Object download(@RequestParam String path, HttpServletResponse response) {
		try {
			File file = new File(serverPath, path);
			if (file.exists()) {
				response.setContentType("application/force-download");// 设置强制下载不打开
				response.addHeader("Content-Disposition", "attachment;fileName=" + file.getName());// 设置文件名
				IoUtils.write(file, response.getOutputStream());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
